/**
 * useradmin demo
 */


layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //用户管理
    table.render({
        elem: '#LAY-user-manage'
        , url: './res/json/useradmin/webuser.js' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'username', title: '用户名', minWidth: 100}
            , {field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
            , {field: 'phone', title: '手机'}
            , {field: 'email', title: '邮箱'}
            , {field: 'sex', width: 80, title: '性别'}
            , {field: 'ip', title: 'IP'}
            , {field: 'jointime', title: '加入时间', sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-webuser'}
        ]]
        , page: true
        , limit: 30
        , height: 'full-320'
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-user-manage)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);

                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑用户'
                , area: ['500px', '450px']
                , id: 'LAY-popup-user-add'
                , success: function (layero, index) {
                    view(this.id).render('user/user/userform', data).done(function () {
                        form.render(null, 'layuiadmin-form-useradmin');

                        //提交
                        form.on('submit(LAY-user-front-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            layui.table.reload('LAY-user-manage'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });


    var tableElem = "LAY-home-admin-list";
    var tableToolsElem = "LAY-home-admin-tools";
    var resetPwdFormElem = "LAY-home-admin-resetpwd-form";
    var resetPwdFormSubmitElem = "LAY-home-admin-resetpwd-form-submit";
    var adminFormElem = "LAY-home-admin-adminform-form";
    var adminFormSubmitElem = "LAY-home-admin-adminform-form-submit";
    //管理员管理
    table.render({
        elem: `#${tableElem}`
        , url: window.globalConfig.url_user //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'user_id', width: 100, title: 'ID', sort: true}
            , {field: 'user_name', title: '用户名', minWidth: 150}
            , {
                field: 'agency_id', title: '身份', minWidth: 100, templet: function (d) {
                    if (d.agency_id == 0) {
                        return "系统管理员"
                    } else if (d.agency_id == 1) {
                        return "普通管理员"
                    }
                }
            }
            , {field: 'phone', title: '手机', minWidth: 150}
            , {field: 'email', title: '邮箱', minWidth: 150}
            , {field: 'last_ip', title: '最后登录IP', minWidth: 100}
            , {field: 'last_login', title: '最后登录时间', minWidth: 200}
            , {field: 'add_time', title: '加入时间', sort: true, minWidth: 200}
            , {field: 'update_time', title: '更新时间', sort: true, minWidth: 200}
            , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}

        ]]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除管理员
            // layer.prompt({
            //     formType: 1
            //     , title: '敏感操作，请验证口令'
            // }, function (value, index) {
            //     layer.close(index);
            layer.confirm('确定删除此管理员？', function (index) {
                admin.req({
                    url: window.globalConfig.url_user + "/" + data.user_id,
                    type: "DELETE",
                    success: function (res) {
                        layer.msg(res.msg)
                        layer.close(index);
                        layui.table.reload(tableElem); //重载表格
                    }
                })
            });
            // });
        } else if (obj.event === 'resetPwd') { // 重置密码
            admin.popup({
                title: '管理员重置密码'
                , area: ['420px', '350px']
                , id: 'LAY-popup-user-add'
                , success: function (layero, index) {
                    view(this.id).render('user/administrators/resetpwd', data).done(function () {
                        form.render(null, resetPwdFormElem);

                        //提交
                        form.on(`submit(${resetPwdFormSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段
                            //确认密码
                            if (field.new_password !== field.repass) {
                                return layer.msg('两次密码输入不一致');
                            }
                            admin.req({
                                url: window.globalConfig.url_password + "/" + field.user_id //实际使用请改成服务端真实接口
                                , type: "PUT"
                                , data: field
                                , done: function (res) {
                                    if (res.code === 0) {
                                        layer.msg('密码已成功重置')
                                    } else {
                                        layer.msg(res.msg)
                                    }
                                    layui.table.reload(tableElem); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });

                        });
                    });
                }
            });
        } else if (obj.event === 'edit') { // 编辑管理员
            admin.popup({
                title: '编辑管理员'
                , area: ['420px', '450px']
                , id: 'LAY-popup-user-add'
                , success: function (layero, index) {
                    data.event = 'edit'
                    view(this.id).render('user/administrators/adminform', data).done(function () {
                        form.render(null, adminFormElem);

                        //提交
                        form.on(`submit(${adminFormSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: window.globalConfig.url_user + "/" + field.user_id,
                                type: "PUT",
                                data: field,
                                success: function (res) {
                                    layer.msg(res.msg)
                                    layui.table.reload(tableElem); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            })

                        });
                    });
                }
            });
        }
    });

    //角色管理
    table.render({
        elem: '#LAY-user-back-role'
        , url: './res/json/useradmin/role.js' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true}
            , {field: 'rolename', title: '角色名'}
            , {field: 'limits', title: '拥有权限'}
            , {field: 'descr', title: '具体描述'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
        ]]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-user-back-role)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此角色？', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '添加新角色'
                , area: ['500px', '480px']
                , id: 'LAY-popup-user-add'
                , success: function (layero, index) {
                    view(this.id).render('user/administrators/roleform', data).done(function () {
                        form.render(null, 'layuiadmin-form-role');

                        //提交
                        form.on('submit(LAY-user-role-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            layui.table.reload('LAY-user-back-role'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });

    exports('useradmin', {})
});